<?php
namespace App\Api\Xhzx;

use PhalApi\Api;


/**
 * 获取代理数量
 * @author dogstar 20200331
 */
class User extends Api {
    public function getRules() {
        return array(
            'getAgent' => array(
                'username' => array('name' => 'username', 'require' => true, 'min' => 1, 'max' => 50, 'desc' => 'username'),
            ),
            'getIncome' => array(
                'username' => array('name' => 'username', 'require' => true, 'min' => 1, 'max' => 50, 'desc' => 'username'),
            )
        );
    }

    /**
     * 获取代理数量
     * @desc 更新帐号数据
     * @return int res 是否更新成功
     */
    public function getAgent() {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');
        
        $username = $this->username;

        $where = array(
            'Invitation' => $username
        );
 
        $recrod = \PhalApi\DI()->notorm->phalapi_user->where($where)->fetchAll();
        if( $recrod ){
            foreach( $recrod as $key => $value ){
                $recrod[$key]['agent'] = \PhalApi\DI()->notorm->phalapi_user->where(array('Invitation' => $value['username']))->count();
                $recrod[$key]['phone_mun'] = $recrod[$key]['phone_mun'] + \PhalApi\DI()->notorm->phone_list->where(array('username' => $value['username']))->count();
            }
            return $recrod;
        }
    }

    /**
     * 获取收汇报
     * @desc 更新帐号数据
     * @return int res 是否更新成功
     */
    public function getIncome() {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');
        
        $username = $this->username;

        $where = array(
            'username' => $username
        );
 
        $recrod = \PhalApi\DI()->notorm->phone_money->where($where)->fetchAll();
        if( $recrod ){
            return $recrod;
        }
    }

}
